package com.jimple.apps.RssClient.backend;

import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Properties;

import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.TreeItem;

import com.jimple.apps.RssClient.ui.UiSetup;
import com.sun.syndication.feed.synd.SyndEntry;
import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.fetcher.FeedFetcher;
import com.sun.syndication.fetcher.impl.FeedFetcherCache;
import com.sun.syndication.fetcher.impl.HashMapFeedInfoCache;
import com.sun.syndication.fetcher.impl.HttpURLFeedFetcher;

public class Reader extends Thread {
	private Properties feeds;

	private UiSetup uis;
	private Display display;

	public Reader(Properties feeds, UiSetup uis, Display display) {
		try {
			this.display = display;
			this.uis = uis;
			this.feeds = feeds;
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void run() {
		super.run();
		try {
			display.asyncExec(new Runnable() {
				public void run() {
					uis.getTitleLabel().setText("Refreshing Feeds....");
					uis.clearTable();
					uis.allowRefresh(false);
				}
			});
			// Iterator urlsIter = urls.iterator();
			Iterator feedsIter = feeds.keySet().iterator();
			while (feedsIter.hasNext()) {
				final String key = (String) feedsIter.next();
				final String thisiter = (String) feeds.getProperty(key);
				System.out.println(thisiter);

				List<SyndEntry> entries = getEntriesFromUrl(thisiter);
				display.asyncExec(new Runnable() {
					public void run() {
						uis.addSiteToTable(key);
					}
				});
				for (int a = 0; a < entries.size(); a++) {
					final SyndEntry entry = entries.get(a);
					// final String urla = entry.getDescription().getValue();
					display.asyncExec(new Runnable() {
						public void run() {
							uis.addItemToTable(uis.getCurrentSite(), entry
									.getTitle(), entry);
						}
					});

				}

			}
			display.asyncExec(new Runnable() {
				public void run() {
					uis.redrawTable();
					TreeItem[] items = uis.getTree().getItems();
					for (int i = 0; i < items.length; i++) {
						items[i].setExpanded(true);
					}
					uis.allowRefresh(true);
					uis.getTitleLabel().setText("");
				}
			});

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private List<SyndEntry> getEntriesFromUrl(String urlString) {
		try {
			URL feedUrl = new URL(urlString);

			FeedFetcherCache feedInfoCache = HashMapFeedInfoCache.getInstance();
			FeedFetcher feedFetcher = new HttpURLFeedFetcher(feedInfoCache);
			SyndFeed feed = feedFetcher.retrieveFeed(feedUrl);

			return feed.getEntries();

		} catch (SocketTimeoutException se) {
			System.err.println("socket timeout: " + urlString);
			se.printStackTrace();
		} catch (ConnectException ce) {
			System.err.println("Connection exception: " + urlString);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new LinkedList<SyndEntry>();
	}
}
